﻿using MediatR;
using ProduceBasicData.Api.Application.Command.ProductionManage.Plan;
using ProduceBasicData.Domain.ProductionManage;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.ProductionManage.Plan
{
    public class DeletePlan_SchedulingCommandHandler : IRequestHandler<DeletePlan_SchedulingCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<Plan_Scheduling> planrepositry;

        public DeletePlan_SchedulingCommandHandler(IBaseRespository<Plan_Scheduling> planrepositry)
        {
            this.planrepositry = planrepositry;
        }

        public async Task<ApiResult<int>> Handle(DeletePlan_SchedulingCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>()
            {
                Code = ApiEnum.Success,
                Msg = "删除排班成功"
            };

            try
            {
                // 根据ID查询排班记录
                var scheduling = await planrepositry.GetModelAsync(request.Id);
                if (scheduling == null)
                {
                    result.Code = ApiEnum.Failed;
                    result.Msg = "排班记录不存在";
                    return result;
                }

                // 软删除
                scheduling.Isdel = true;
                result.Data = await planrepositry.Update(scheduling);
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.Failed;
                result.Msg = "删除失败：" + ex.Message;
            }

            return result;
        }
    }
}